package com.example.gameapplication.activity;

import android.animation.Animator;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.gameapplication.R;
import com.example.gameapplication.database.DataUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ControlDialogActivity extends AppCompatActivity {

    @BindView(R.id.et_1)
    EditText et1;
    @BindView(R.id.et_2)
    EditText et2;
    @BindView(R.id.et_3)
    EditText et3;
    @BindView(R.id.et_4)
    EditText et4;
    @BindView(R.id.et_5)
    EditText et5;
    @BindView(R.id.et_6)
    EditText et6;
    @BindView(R.id.et_7)
    EditText et7;
    @BindView(R.id.et_8)
    EditText et8;
    @BindView(R.id.et_9)
    EditText et9;
    @BindView(R.id.et_10)
    EditText et10;
    @BindView(R.id.et_11)
    EditText et11;
    @BindView(R.id.btn_sure)
    TextView btnSure;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    @BindView(R.id.sw_invincible)
    CheckBox swInvincible;
    @BindView(R.id.et_12)
    EditText et12;
    @BindView(R.id.et_13)
    EditText et13;
    @BindView(R.id.et_14)
    EditText et14;
    @BindView(R.id.et_15)
    EditText et15;
    @BindView(R.id.et_16)
    EditText et16;
    @BindView(R.id.et_17)
    EditText et17;
    private Animator animator;
    private View inflate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = LayoutInflater.from(this).inflate(R.layout.activity_control_dialog, null);
        setContentView(inflate);
        ButterKnife.bind(this);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        //获取屏幕宽高
        lp.width = DataUtils.getGameViewWidth() / 20 * 19;
        lp.height = DataUtils.getGameViewHight() * 4 / 5;
        window.setAttributes(lp);
        initdata();
    }


    @Override
    protected void onResume() {
        super.onResume();
        inflate.post(new Runnable() {
            @Override
            public void run() {
                animator = ViewAnimationUtils.createCircularReveal(inflate, 0, 0, 0, getResources().getDisplayMetrics().heightPixels).setDuration(1000);
                inflate.setVisibility(View.VISIBLE);
                animator.start();
            }
        });

    }

    private void initdata() {
        swInvincible.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); //20SP
        swInvincible.setChecked(DataUtils.isInvincible());
        et1.setText(DataUtils.getSmallMaxSum() + "");
        et2.setText(DataUtils.getMiddleMaxSum() + "");
        et3.setText(DataUtils.getBigMaxSum() + "");
        et4.setText(DataUtils.getBossMaxSum() + "");

        et5.setText(DataUtils.getSmallGrow() + "");
        et6.setText(DataUtils.getMiddleGraw() + "");
        et7.setText(DataUtils.getBigGraw() + "");
        et8.setText(DataUtils.getBossGraw() + "");

        et9.setText(DataUtils.getSmallCd() + "");
        et10.setText(DataUtils.getMiddleCd() + "");
        et11.setText(DataUtils.getBigCd() + "");
        et12.setText(DataUtils.getBossCd() + "");

        et13.setText(DataUtils.getSkillBulletMult() + "");
        et14.setText(DataUtils.getWindMillBulletStartLv() + "");
        et15.setText(DataUtils.getWindMillBulletMult() + "");
        et16.setText(DataUtils.getMyPlaneBulletSize() + "");
        et17.setText(DataUtils.getMyBulletGrow() + "");
    }

    @OnClick({R.id.btn_sure, R.id.btn_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_sure:
                save();
                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }

    private void save() {
        if (str2int(et1.getText().toString()) > 100) {
            Toast.makeText(this, "同时出现的小型战机不可以超过100", Toast.LENGTH_SHORT).show();
            return;
        }
        if (str2int(et2.getText().toString()) > 100) {
            Toast.makeText(this, "同时出现的中型战机不可以超过100", Toast.LENGTH_SHORT).show();
            return;
        }
        if (str2int(et3.getText().toString()) > 100) {
            Toast.makeText(this, "同时出现的大型战机不可以超过100", Toast.LENGTH_SHORT).show();
            return;
        }
        if (str2int(et4.getText().toString()) > 100) {
            Toast.makeText(this, "同时出现的Boss不可以超过100", Toast.LENGTH_SHORT).show();
            return;
        }
        if (str2int(et16.getText().toString()) > 50) {
            Toast.makeText(this, "弹夹数量不可以超过50", Toast.LENGTH_SHORT).show();
            return;
        }
        DataUtils.setInvincible(swInvincible.isChecked());
        DataUtils.setSmallMaxSum(str2int(et1.getText().toString()));
        DataUtils.setMiddleMaxSum(str2int(et2.getText().toString()));
        DataUtils.setBigMaxSum(str2int(et3.getText().toString()));
        DataUtils.setBossMaxSum(str2int(et4.getText().toString()));

        DataUtils.setSmallGrow(str2int(et5.getText().toString()));
        DataUtils.setMiddleGraw(str2int(et6.getText().toString()));
        DataUtils.setBigGraw(str2int(et7.getText().toString()));
        DataUtils.setBossGraw(str2int(et8.getText().toString()));


        DataUtils.setSmallCd(str2long(et9.getText().toString()));
        DataUtils.setMiddleCd(str2long(et10.getText().toString()));
        DataUtils.setBigCd(str2long(et11.getText().toString()));
        DataUtils.setBossCd(str2long(et12.getText().toString()));

        DataUtils.setSkillBulletMult(str2int(et13.getText().toString()));
        DataUtils.setWindMillBulletStartLv(str2int(et14.getText().toString()));
        DataUtils.setWindMillBulletMult(str2int(et15.getText().toString()));
        DataUtils.setMyPlaneBulletSize(str2int(et16.getText().toString()));
        DataUtils.setMyBulletGrow(str2int(et17.getText().toString()));
        finish();
    }

    private int str2int(String intString) {
        int i = 0;
        try {
            i = Integer.parseInt(intString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    private long str2long(String longString) {
        long l = 0;
        try {
            l = Long.parseLong(longString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

}
